package com.ikingtech.platform.service.log.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.log.model.SystemLogQueryParamDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.log.entity.SystemLogDO;
import com.ikingtech.platform.service.log.mapper.SystemLogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 操作日志实现类
 *
 * @author wangbo
 */
@Service
@RequiredArgsConstructor
public class SystemLogService extends ServiceImpl<SystemLogMapper, SystemLogDO> {

    public PageResult<SystemLogDO> listPage(SystemLogQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<SystemLogDO>lambdaQuery()
                .like(Tools.Str.isNotBlank(queryParam.getMethod()), SystemLogDO::getMethod, queryParam.getMethod())
                .like(Tools.Str.isNotBlank(queryParam.getPackageName()), SystemLogDO::getPackageName, queryParam.getPackageName())
                .like(Tools.Str.isNotBlank(queryParam.getTraceId()), SystemLogDO::getTraceId, queryParam.getTraceId())
                .ge(null != queryParam.getExecuteStartTime(), SystemLogDO::getExecuteTime, queryParam.getExecuteStartTime())
                .le(null != queryParam.getExecuteEndTime(), SystemLogDO::getExecuteTime, queryParam.getExecuteEndTime())
                .orderByDesc(SystemLogDO::getCreateTime)));
    }

    public void removeByTenantCode(String tenantCode) {
        this.remove(Wrappers.<SystemLogDO>query().lambda().eq(SystemLogDO::getTenantCode, tenantCode));
    }
}
